﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basic4Net.BusinessRules;
using Basic4Net.Controls;
using Basic4Net.Entities;
using Basic4Net.OAWeb.Base;
using Basic4Net.BusinessRules.View;

namespace Basic4Net.OAWeb.Module_Base
{
    public partial class TeachPostRelation : BasePage
    {
        protected IList<TeachPostEntity> teacherList = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDropList();
              //  InitTeachData();
                Bind(0, aspNetPager.PageSize);
            }
        }

        /// <summary>
        /// 初始化职务下拉框
        /// </summary>
        private void InitDropList()
        {

            List<t_Post> list =  Post.PostInstance.FindAll(SchoolMark);

            t_Post item = new t_Post();
            item.PostName = "无职务";
            item.RID = -1;
            list.Add(item);

            dropList2.DataSource = list;
            dropList2.DataValueField = "RID";
            dropList2.DataTextField = "PostName";
            dropList2.DataBind();

            //添加一个“其他”选项

            t_Post item1 = new t_Post();
            item1.PostName = "--所有--";
            item1.RID = 0;
            list.Add(item1);
            dropList1.DataSource = list;
            dropList1.DataValueField = "RID";
            dropList1.DataTextField = "PostName";
            dropList1.DataBind();
            dropList1.SelectedValue = "0";
        }

       

        /// <summary>
        /// 分页控件事件
        /// </summary>
        protected void AspNetPagerPageChanged(object sender, BNPager.PageChangedEventArgs e)
        {
            if (e.NewPageIndex <= 0)
                return;
            aspNetPager.PageIndex = e.NewPageIndex;
            Bind((e.NewPageIndex - 1) * aspNetPager.PageSize, aspNetPager.PageSize);
        }

 
        private void Bind(int startAt,int step)
        {
            //string teachName = TeachName.Text;
            string teachCode = TeachCode.Text;
            int postId = dropList1.SelectedItem.Value == null ? -1 : Convert.ToInt32(dropList1.SelectedItem.Value);
            int count = 0;
          //  List<object> newList = new List<object>();
            if(postId >= 0)
                teacherList = PostRelation.PostRelationInstance.GetTeachInfoByPost(startAt,step,postId,teachCode,SchoolMark,ref count); 
            else
                teacherList = PostRelation.PostRelationInstance.GetTeachInfoByPost(startAt, step, null, teachCode, SchoolMark, ref count);

            aspNetPager.RecordCount = count;
            Repeater1.DataSource = teacherList;
            Repeater1.DataBind();

        }

        protected void btnItemBind_Click(object sender, EventArgs e)
        {
            Bind((aspNetPager.PageIndex - 1) * aspNetPager.PageSize, aspNetPager.PageSize);
        }


        /// <summary>
        /// 添加教师职务
        /// </summary>
        /// <param name="arrTeacher"></param>
        /// <param name="postId"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [System.Web.Services.WebMethod]
        public static int AddTeachPost(int[] arrTeacher,int postId)
        {
            if (arrTeacher.Count() == 0 || postId <= 0)
                return -1;

            bool b = false;
            b = PostRelation.PostRelationInstance.AddTeachPostRelation(arrTeacher, postId, SchoolMark);
            if (b)
                return 1;
            else
                return -1;
        }


        /// <summary>
        /// 设置教师职务
       /// </summary>
       /// <param name="arrTeacher"></param>
       /// <param name="postId"></param>
       /// <param name="type">1-追加职务 2-修改职务</param>
       /// <returns></returns>
        [System.Web.Services.WebMethod]
        public static int UpdateTeachPost(int[] arrTeacher, int postId, int[] oldPostId)
        {
            if (arrTeacher.Count() == 0)
                return -1;

            bool b = false;

            //if (oldPostId > 0)
            //{
                b = PostRelation.PostRelationInstance.UpdateTeachPostRelation(arrTeacher, postId, oldPostId, SchoolMark);
            //}
            //else
            //{
            //    b = PostRelation.PostRelationInstance.AddTeachPostRelation(arrTeacher, postId, SchoolMark);
            //} 
            if(b)
               return 1;
            else
               return -1;
        }

     }

}
